$(function(){
	mui.init();
	mui('.mui-bar-tab').on('tap','a',function(){document.location.href=this.href;});
});
//普通单图上传
layui.use('upload', function(){
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        elem: '#_img_upload' //绑定元素
        ,url: '/index/fileupload/upload' //上传接口
        ,accept:'images/*'
        ,acceptMime: 'image/*',
        before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.msg('上传中···',{time:60000,icon: 16,shade: 0.3});
        }
        ,done: function(res){
            //上传完毕回调
            if(res.code>0){
                layer.msg(res.message);
            }
            $('._img_url').val(res.url);
            $('._img_show').attr('src', res.url);
            layer.msg('上传成功');
            layer.closeAll();
        }
        ,error: function(){
            //请求异常回调
            layer.msg('文件上传失败，请稍后重试或联系管理员');
        }
    });
});
//普通文件上传
layui.use('upload', function(){
    var upload = layui.upload;
    //执行实例
    var uploadInst = upload.render({
        elem: '._file_uploads' //绑定元素
        ,url: '/index/fileupload/upload' //上传接口
        ,accept:'file'
        ,data:{path:'file'},
        before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.msg('上传中···',{time:60000,icon: 16,shade: 0.3});
        }
        //,acceptMime: 'file/*'
        ,done: function(res){
            //上传完毕回调
            if(res.code>0){
                layer.msg(res.message);
            }
            let item = this.item.siblings('._file_url');
            item.val(res.url);
            let action =  item.parent().children('.fabu-upload').children('._action');
            let action1 =  item.parent().children('.fabu-upload').children('._action1');
            action1.attr('data-url',res.url);
            action.html('上传成功!');
            action1.html('上传文件名称：'+res.name);
            layer.closeAll();
            layer.msg('上传成功');
        }
        ,error: function(){
            //请求异常回调
            layer.msg('文件上传失败，请稍后重试或联系管理员');
        }
    });
});

/**
 *
 * @param type 类型1点赞 2 收藏
 * @param topic_id
 * @param obj
 */
function topic_appreciate(type,topic_id,obj) {
    var oThisclass = obj.className;
    if(oThisclass=='active'){
        var do_appreciate =2;
    }else{
        do_appreciate =1;
    }
    var sum = parseInt($(obj).find('i').html());
    $.ajax({
        url:'/index/appreciate/topic_appreciate',
        data:{
        topic_id:topic_id,
            type:type,
            do_appreciate:do_appreciate
    },
    success:function (data) {
        if(do_appreciate ==1){
            obj.setAttribute('class','active');
            if(type==1) {
                $('.topic_appreciate_sum').html(sum + 1);
            }
        }else{
            obj.setAttribute('class','');
            if(type==1) {
                $('.topic_appreciate_sum').html(sum - 1);
            }
        }
        console.log(data);
        layer.msg(data.msg);
    }
})
}

$('._action1').click(function () {
   var  href = $(this).attr('data-url');
   location.href=href;
});

function get_file(obj) {
    var href = $(obj).attr('data-url');
    if(check_is_img(href)){
        html = '<img src="'+href+'" width="100%" height="auto">';
        type = 1;
    }else if(check_is_txt(href)){
        type =2;
        html = href
    }else{
        type=2;
        html =href;
    }
    layer.open({
        type: type,
        area:['90%','90%'],
        content:html,
    });
    if((!check_is_img(href))&&(!check_is_txt(href))){
        layer.closeAll();
    }
}

function check_is_img(url) {
    return (url.match(/\.(jpeg|jpg|gif|png)$/) != null)
}
function check_is_txt(url) {
    return (url.match(/\.(txt)$/) != null)
}